package com.jsbj.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.jsbj.base.ResultBean;
import com.jsbj.framework.controller.BaseController;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysTenant;
import com.jsbj.user.entities.SysTenantPermission;
import com.jsbj.user.entities.TenantPermissionParams;
import com.jsbj.user.service.ISysTenantPermissionService;
import com.jsbj.user.service.ISysTenantService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租户菜单表 前端控制器
 * </p>
 *
 * @author jsbj
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/sys-tenant-permission")
@Api(value = "租户菜单配置", description = "租户菜单配置", tags = "租户菜单配置")
@ApiIgnore
public class SysTenantPermissionController extends BaseController {

    @Autowired
    private ISysTenantPermissionService sysTenantPermissionService;

    @Autowired
    private ISysTenantService sysTenantService;

    @ApiIgnore
    @ApiOperation("根据用户id+租户编码查询所有有权限菜单（包含页面、树形 不包含按钮）")
    @ApiImplicitParam(name = "inputParam", value = "用户id+租户编码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("/getByUserIdAndTenant")
    public ResultBean<List<SysTenantPermission>> getByUserIdAndTenant(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return new ResultBean<List<SysTenantPermission>>(sysTenantPermissionService.getByUserIdAndTenant(object));
    }

    @ApiIgnore
    @ApiOperation("根据用户id+租户编码+菜单编码查询子节点按钮")
    @ApiImplicitParam(name = "inputParam", value = "用户id+租户编码+菜单编码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("/getButtonByUserIdAndTenantAndMenuCode")
    public ResultBean<List<SysTenantPermission>> getButtonByUserIdAndTenantAndMenuCode(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return new ResultBean<List<SysTenantPermission>>(sysTenantPermissionService.getButtonByUserIdAndTenantAndMenuCode(object));
    }

    @ApiIgnore
    @ApiOperation("根据用户id+租户编码查询配置中心菜单")
    @ApiImplicitParam(name = "inputParam", value = "用户id+租户编码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("/getPZByUserIdAndTenant")
    public ResultBean<List<SysTenantPermission>> getPZByUserIdAndTenant(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return new ResultBean<List<SysTenantPermission>>(sysTenantPermissionService.getPZByUserIdAndTenant(object));
    }

    @ApiIgnore
    @ApiOperation("根据用户id+租户编码查询所有有权限菜单（包含页面、树形 不包含按钮）PC改造-新")
    @ApiImplicitParam(name = "inputParam", value = "用户id+租户编码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("/getByUserIdAndTenantPC")
    public ResultBean<Map<String, Object>> getByUserIdAndTenantPC(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return new ResultBean<Map<String, Object>>(sysTenantPermissionService.getByUserIdAndTenantPC(object));
    }

    @ApiOperation("根据用户id+租户编码查询所有有权限菜单（包含页面、树形 不包含按钮）PC改造-更新")
    @ApiImplicitParam(name = "inputParam", value = "用户id+租户编码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("/getByUserIdAndTenantPCNew")
    public ResultBean<List<LinkedHashMap<String, Object>>> getByUserIdAndTenantPCNew(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return new ResultBean<List<LinkedHashMap<String, Object>>>(sysTenantPermissionService.getByUserIdAndTenantPCNew(object));
    }

    @ApiOperation("查询列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("/getListData")
    public ResultBean<List<SysTenantPermission>> getListData(@RequestBody String inParam) {
        JSONObject object = JSONObject.parseObject(inParam);
        String tenantCode = object.getString("tenantCode");
        if (StringUtils.isEmpty(tenantCode)) {
            return new ResultBean<>().build("租户编码不能为空！");
        }
        return new ResultBean<List<SysTenantPermission>>(sysTenantPermissionService.getListData(object));
    }

    @ApiOperation("已选菜单编码（弹框默认选中，不可修改）")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping("alraedyChoose")
    public ResultBean<List<String>> alraedyChoose(@RequestParam String tenantCode) {
        return new ResultBean<List<String>>(sysTenantPermissionService.alraedyChoose(tenantCode));
    }

    @ApiOperation("引用租户菜单保存")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping("saveOrUpdateYY")
    public ResultBean<JSONObject> saveOrUpdateYY(@RequestBody TenantPermissionParams tenantPermissionParams) {
        return sysTenantPermissionService.saveOrUpdateYY(tenantPermissionParams);
    }

    @ApiOperation("新增子节点保存")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping("saveOrUpdateZJD")
    public ResultBean<JSONObject> saveOrUpdateZJD(@RequestBody TenantPermissionParams tenantPermissionParams) {
        return sysTenantPermissionService.saveOrUpdateZJD(tenantPermissionParams);
    }

    @ApiOperation("新增目录保存")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping("saveOrUpdateML")
    public ResultBean<JSONObject> saveOrUpdateML(@RequestBody TenantPermissionParams tenantPermissionParams) {
        return sysTenantPermissionService.saveOrUpdateML(tenantPermissionParams);
    }

    @ApiOperation("查看详情")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "id（行）", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenantPermission.class)})
    @PostMapping("getDetail")
    public ResultBean<SysTenantPermission> getDetail(@RequestParam String id) {
        return new ResultBean<SysTenantPermission>(sysTenantPermissionService.getDetail(id));
    }

    @ApiOperation("删除数据")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "id（行）", required = true)}
    )
    @PostMapping("deleteData")
    public ResultBean<JSONObject> deleteData(@RequestParam String id) {
        return sysTenantPermissionService.deleteData(id);
    }

    @ApiOperation("编辑保存")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping("updateData")
    public ResultBean<JSONObject> updateData(@RequestBody SysTenantPermission sysTenantPermission) {
        return sysTenantPermissionService.updateData(sysTenantPermission);
    }

    @ApiOperation("引用租户菜单-参考项目")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenant.class)})
    @PostMapping("/getCopyTenant")
    public ResultBean<List<SysTenant>> getCopyTenant(@RequestBody String inParam) {
        return new ResultBean<List<SysTenant>>(sysTenantService.getCopyTenant(inParam));
    }

    @PostMapping("/getMenuNameByCode")
    public String getMenuNameByCode(@RequestParam String tenantCode, @RequestParam String menuCode) {
        return sysTenantPermissionService.getMenuNameByCode(tenantCode, menuCode);
    }

    @PostMapping("/getMLListData")
    public ResultBean<List<SysTenantPermission>> getMLListData(@RequestBody String inParam) {
        JSONObject object = JSONObject.parseObject(inParam);
        String tenantCode = object.getString("tenantCode");
        if (StringUtils.isEmpty(tenantCode)) {
            return new ResultBean<>().build("租户编码不能为空！");
        }
        return new ResultBean<List<SysTenantPermission>>(sysTenantPermissionService.getMLListData(object));
    }


}

